// pages/alarm/index.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        unreadList: [],
        unread: 0,
        read: 0,
        readList: [],
        showRead: "0",
        orderLevel: 'desc',
        orderTime: 'desc'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        let query = {
            level: this.data.orderLevel,
            time: this.data.orderTime
        }
        wx.showLoading({
            title: '数据加载中',
        })
        app.api.getAlarm(query).then(res => {
            let data = res.data;
            if (Array.isArray(data)) {
                let unreadList = data[0]
                let readList = data[1]
                this.setData({
                    unread: unreadList.length,
                    read: readList.length,
                    unreadList: unreadList,
                    readList: readList
                })
            }
            wx.hideLoading()
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    goDetail(e) {
        let index = e.currentTarget.dataset.index;
        if (index != null) {
            let alarm = null
            if (this.data.showRead == '0') {
                alarm = this.data.unreadList[index]
            } else if (this.data.showRead == '1') {
                alarm = this.data.readList[index]
            }
            if (alarm != null) {
                wx.navigateTo({
                    url: '/pages/alarm-detail/index?faultId=' + alarm.faultId + '&isRead=' + this.data.showRead
                })
            }
        }
    },
    changeRead(e) {
        let type = e.currentTarget.dataset.type;
        if (type != null) {
            if (type != this.data.showRead) {
                this.setData({
                    showRead: type
                })
            }
        }
    },
    changeLevel() {
        if (this.data.orderLevel == 'asc') {
            this.setData({
                orderLevel: "desc"
            })
        } else {
            this.setData({
                orderLevel: "asc"
            })
        }
        let query = {
            level: this.data.orderLevel,
            time: this.data.orderTime
        }
        wx.showLoading({
            title: '数据加载中',
        })
        app.api.getAlarm(query).then(res => {
            let data = res.data;
            if (Array.isArray(data)) {
                let unreadList = data[0]
                let readList = data[1]
                this.setData({
                    unread: unreadList.length,
                    read: readList.length,
                    unreadList: unreadList,
                    readList: readList
                })
            }
            wx.hideLoading()
        })
    },
    changeTime() {
        if (this.data.orderTime == 'asc') {
            this.setData({
                orderTime: "desc"
            })
        } else {
            this.setData({
                orderTime: "asc"
            })
        }
        let query = {
            level: this.data.orderLevel,
            time: this.data.orderTime
        }
        wx.showLoading({
            title: '数据加载中',
        })
        app.api.getAlarm(query).then(res => {
            let data = res.data;
            if (Array.isArray(data)) {
                let unreadList = data[0]
                let readList = data[1]
                this.setData({
                    unread: unreadList.length,
                    read: readList.length,
                    unreadList: unreadList,
                    readList: readList
                })
            }
            wx.hideLoading()
        })
    }
})